package com.example.permissionservice.controller;

import com.example.permissionservice.service.UserTokenService;
import com.myprojects.common.pojo.Resource;
import com.myprojects.common.pojo.ResourceParent;
import com.example.permissionservice.service.PermissionService;
import com.myprojects.common.dto.Msg;
import com.myprojects.common.pojo.UserWithRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
//@RequestMapping("/checkPermission")
public class PermissionController {

    @Autowired
    PermissionService permissionService;

    @Autowired
    UserTokenService userTokenService;

    /**
     * 该方法用于通过用户id来查询用户拥有的所有资源(权限)
     */
    @RequestMapping(value = "/perm", method = RequestMethod.GET)
    @ResponseBody
    public Msg getUserByToken(@RequestHeader(value = "accessToken") String accessToken) {
        Msg result;//要返回的结果
        //获取全部用户信息和权限
        UserWithRole user = permissionService.getUserByUserToken(accessToken);

        if (user != null){
            //生成返回成功的结果
            result = Msg.succeed();
            //将user加入返回的结果中
            result.addData("user",user);
        }else {
            result = Msg.failed();
            result.setMsg("服务端处理异常!");
        }
        return result;
    }

    /**
     * 该方法需配合网关模块使用，该方法通过用户请求和token来检查用户拥有的所有资源(权限)
     */
    @RequestMapping(value = "/check-request", method = RequestMethod.POST)
    @ResponseBody
    public boolean checkUserRequest(@RequestHeader(value = "accessToken") String accessToken,
                                @RequestBody String requestURI) {
        boolean checkResult = permissionService.checkUserRequest(accessToken,requestURI);
        return checkResult;
    }

}
